<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;

class Coupon extends Model
{
    // 设置json类型字段
    protected $json = [];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;
    
    // 搜索器
    public function searchKeywordAttr($query, $value)
    {
    	if (! empty($value)) {
	        $query->where("id|name|amount|number","like", "%" . $value . "%");
	    }
    }
    
    public function searchDateAttr($query, $value, $array)
    {
        if (! empty($value)) { 
            $query->whereBetweenTime("end_time", $value[0], $value[1]);
        }
    }
    
    // 修改器、获取器
    public function getMerchantIdAttr($value, $array)
    {
        return $value ? explode(",", $value) : [];
    }
    
    public function setMerchantIdAttr($value, $array)
    {
        return implode(",", $value);
    }

	public function setStartTimeAttr($value, $array)
    {
        return empty($value) ? date("Y-m-d H:i:s") : $value;
    }

	public function setEndTimeAttr($value, $array)
    {
        return empty($value) ? date("Y-m-d H:i:s") : $value;
    }
    public function merchant()
    {
        return $this->hasOne('Merchant', 'merchant_id');
    }
}